package com.supermarket.controller;

import com.supermarket.entity.Commodity;
import com.supermarket.service.CommodityService;
import com.supermarket.common.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/commodities")
public class CommodityController {

    @Resource
    private CommodityService commodityService;
    
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增商品
     */
    @PostMapping("/add")
    public int addCommodity(@RequestBody Commodity commodity) {
        return commodityService.addCommodity(commodity);
    }

    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public int updateCommodity(@PathVariable Integer id, @RequestBody Commodity commodity) {
        commodity.setId(id);
        return commodityService.updateCommodity(commodity);
    }

    /**
     * 根据ID查询商品
     */
    @GetMapping("/{id}")
    public Commodity getCommodityById(@PathVariable Integer id) {
        return commodityService.getCommodityById(id);
    }

    /**
     * 条件查询商品
     */
    @GetMapping
    public List<Commodity> searchCommodities(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) List<String> categoryTag) {
        return commodityService.searchCommodities(name, status, categoryTag);
    }
    
    /**
     * 获取热门商品
     */
    @GetMapping("/hot")
    public List<Commodity> getHotCommodities(
            @RequestParam(value = "limit", defaultValue = "10") int limit) {
        return commodityService.getHotCommodities(limit);
    }

    /**
     * 更新分类Tags
     */
    @PutMapping("/{id}/category-tags")
    public int updateCategoryTags(@PathVariable Integer id, @RequestBody List<String> categoryId) {
        return commodityService.updateCategoryTags(id, categoryId);
    }

    /**
     * 更新商品图片
     */
    @PutMapping("/{id}/images")
    public int updateImages(@PathVariable Integer id, @RequestBody List<String> image) {
        return commodityService.updateImages(id, image);
    }

    /**
     * 分页条件查询商品
     */
    @GetMapping("/page")
    public Map<String, Object> searchCommoditiesWithPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) List<String> categoryTag,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {
        return commodityService.searchCommoditiesWithPage(pageNum, pageSize, name, status, categoryTag, sortField, sortOrder);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public int deleteCommodity(@PathVariable Integer id) {
        return commodityService.deleteCommodity(id);
    }
}